package org.apache.tomcat.util;

import java.util.StringJoiner;

public class CallerUtils {


    public static String getCaller(int level) {
        if (level <= 0) {
            level = 1;
        }

        StackTraceElement[] stElements = Thread.currentThread().getStackTrace();
        final int length = stElements.length;
        if (level >= length - 3) {
            level = length - 3;
        }
        final StringJoiner stringJoiner = new StringJoiner(" -> ");

        for (int i = 1; i <= level; i++) {
            final StackTraceElement stElement = stElements[2 + i];
            stringJoiner.add(stElement.toString());
        }

        return stringJoiner.toString();
    }

}
